<template>
  <div class="members-list">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h2>社区成员管理</h2>
        <p class="header-subtitle">管理系统中各社区的成员信息</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 筛选和操作区域 -->
      <div class="filter-section">
        <div class="filter-group">
          <label for="community-select">选择社区：</label>
          <select 
            id="community-select"
            v-model="selectedCommunityId" 
            @change="getMembers"
            class="community-select"
          >
            <option value="">请选择社区</option>
            <option v-for="item in communityList" 
                    :key="item.communityId" 
                    :value="item.communityId">
              {{ item.name }}
            </option>
          </select>
        </div>
        <button class="add-btn" @click="showAddDialog">
          <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
            <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
          </svg>
          添加成员
        </button>
      </div>

      <!-- 成员列表卡片 -->
      <div class="members-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
            </svg>
          </div>
          <h3>成员列表</h3>
        </div>

        <div class="card-content">
          <div v-if="loading" class="loading">
            <div class="spinner"></div>
            <span>加载中...</span>
          </div>
          <div v-else-if="!selectedCommunityId" class="empty-state">
            <svg viewBox="0 0 24 24" width="48" height="48" fill="currentColor">
              <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z"/>
            </svg>
            <p>请选择社区查看成员</p>
          </div>
          <div v-else-if="membersList.length === 0" class="empty-state">
            <svg viewBox="0 0 24 24" width="48" height="48" fill="currentColor">
              <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z"/>
            </svg>
            <p>该社区暂无成员</p>
          </div>
          <table v-else class="members-table">
            <thead>
              <tr>
                <th>用户名</th>
                <th>真实姓名</th>
                <th>联系电话</th>
                <th>创建时间</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="member in membersList" :key="member.userId">
                <td>
                  <div class="user-info">
                    <div class="avatar">
                      <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                        <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                      </svg>
                    </div>
                    {{ member.username }}
                  </div>
                </td>
                <td>{{ member.realName }}</td>
                <td>{{ member.phone }}</td>
                <td>{{ member.createTime }}</td>
                <td>
                  <div class="action-buttons">
                    <button class="edit-btn" @click="showEditDialog(member)">
                      <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
                        <path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/>
                      </svg>
                      编辑
                    </button>
                    <button class="delete-btn" @click="handleDelete(member.userId)">
                      <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
                        <path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>
                      </svg>
                      删除
                    </button>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- 添加/编辑对话框 -->
    <div class="dialog-overlay" v-if="showDialog" @click="closeDialog">
      <div class="dialog" @click.stop>
        <div class="dialog-header">
          <h3>{{ isEdit ? '编辑成员' : '添加成员' }}</h3>
          <button class="close-btn" @click="closeDialog">
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
            </svg>
          </button>
        </div>
        <form @submit.prevent="handleSubmit" class="dialog-form">
          <div class="form-item">
            <label>用户名：</label>
            <div class="input-group">
              <div class="input-icon">
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                </svg>
              </div>
              <input 
                v-model="formData.username" 
                required 
                :disabled="isEdit"
                placeholder="请输入用户名"
              />
            </div>
          </div>
          <div class="form-item" v-if="!isEdit">
            <label>密码：</label>
            <div class="input-group">
              <div class="input-icon">
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z"/>
                </svg>
              </div>
              <input 
                v-model="formData.password" 
                type="password" 
                required
                placeholder="请输入密码"
              />
            </div>
          </div>
          <div class="form-item">
            <label>真实姓名：</label>
            <div class="input-group">
              <div class="input-icon">
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M9 11H7v6h2v-6zm4 0h-2v6h2v-6zm4 0h-2v6h2v-6zm2.5-9H19v4h-4V2h-1.5v4h-4V2H8v4H4v16h4v-2h2v2h4v-2h2v2h4v-2h2v2h4V6h-4V2zm-13 9h2v-2h-2v2zm4 0h2v-2h-2v2zm4 0h2v-2h-2v2z"/>
                </svg>
              </div>
              <input 
                v-model="formData.realName" 
                required
                placeholder="请输入真实姓名"
              />
            </div>
          </div>
          <div class="form-item">
            <label>联系电话：</label>
            <div class="input-group">
              <div class="input-icon">
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/>
                </svg>
              </div>
              <input 
                v-model="formData.phone" 
                required
                placeholder="请输入联系电话"
              />
            </div>
          </div>
          <div class="dialog-footer">
            <button type="button" class="cancel-btn" @click="closeDialog">
              <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
              </svg>
              取消
            </button>
            <button type="submit" class="submit-btn">
              <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
              </svg>
              确定
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '../../utils/request'

const loading = ref(false)
const selectedCommunityId = ref('')
const communityList = ref([])
const membersList = ref([])
const showDialog = ref(false)
const isEdit = ref(false)
const formData = ref({
  userId: null,
  username: '',
  password: '',
  realName: '',
  phone: '',
  roleType: 'community'
})

// 获取社区列表
const getCommunityList = async () => {
  try {
    const res = await request.get('/admin/getCommunityList')
    communityList.value = res.data
  } catch (error) {
    console.error('获取社区列表失败:', error)
  }
}

// 获取社区成员
const getMembers = async () => {
  if (!selectedCommunityId.value) {
    membersList.value = []
    return
  }
  
  loading.value = true
  try {
    const res = await request.get(`/admin/getCommunityUsers/${selectedCommunityId.value}`)
    membersList.value = res.data
  } catch (error) {
    console.error('获取成员列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 显示添加对话框
const showAddDialog = () => {
  if (!selectedCommunityId.value) {
    alert('请先选择社区')
    return
  }
  
  isEdit.value = false
  formData.value = {
    userId: null,
    username: '',
    password: '',
    realName: '',
    phone: '',
    roleType: 'community',
    communityId: selectedCommunityId.value
  }
  showDialog.value = true
}

// 显示编辑对话框
const showEditDialog = (member) => {
  isEdit.value = true
  formData.value = { 
    ...member,
    password: '' // 编辑时不需要密码
  }
  showDialog.value = true
}

// 关闭对话框
const closeDialog = () => {
  showDialog.value = false
  formData.value = {
    userId: null,
    username: '',
    password: '',
    realName: '',
    phone: '',
    roleType: 'community'
  }
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (isEdit.value) {
      await request.put('/admin/updateCommunityUser', formData.value)
    } else {
      await request.post('/admin/addCommunityUser', formData.value)
    }
    await getMembers()
    closeDialog()
  } catch (error) {
    console.error('操作失败:', error)
  }
}

// 删除成员
const handleDelete = async (userId) => {
  if (confirm('确定要删除该成员吗？')) {
    try {
      await request.delete(`/admin/deleteCommunityUser/${userId}`)
      await getMembers()
    } catch (error) {
      console.error('删除失败:', error)
    }
  }
}

onMounted(() => {
  getCommunityList()
})
</script>

<style scoped>
.members-list {
  padding: 24px;
  background: linear-gradient(135deg, #f0f7ff 0%, #e6f2ff 100%);
  min-height: 100vh;
}

.page-header {
  margin-bottom: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  padding: 20px;
}

.page-header h2 {
  font-size: 24px;
  color: #2c3e50;
  margin: 0 0 4px 0;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.main-content {
  max-width: 1800px;
  margin: 0 auto;
}

.filter-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.filter-group label {
  font-weight: 500;
  color: #2c3e50;
}

.community-select {
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  min-width: 400px;
  background: #fff;
  font-size: 14px;
  transition: all 0.3s;
}

.community-select:focus {
  outline: none;
  border-color: #4facfe;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.add-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 2px 6px rgba(79, 172, 254, 0.3);
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.4);
}

.members-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  overflow: hidden;
}

.card-header {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  width: 20px;
  height: 20px;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.card-content {
  padding: 24px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #6c757d;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e9ecef;
  border-top: 3px solid #4facfe;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #6c757d;
}

.empty-state svg {
  color: #ced4da;
  margin-bottom: 16px;
}

.empty-state p {
  margin: 0;
  font-size: 16px;
}

.members-table {
  width: 100%;
  border-collapse: collapse;
}

.members-table th,
.members-table td {
  padding: 16px 12px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.members-table th {
  background: #fafafa;
  font-weight: 600;
  color: #495057;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #e3f2fd;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4facfe;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.action-buttons button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
}

.edit-btn {
  background: #e3f2fd;
  color: #1976d2;
}

.edit-btn:hover {
  background: #bbdefb;
}

.delete-btn {
  background: #f8f0f1;
  color: #dc3545;
}

.delete-btn:hover {
  background: #f5c6cb;
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: #fff;
  border-radius: 12px;
  width: 500px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  color: #6c757d;
  transition: background 0.2s ease;
}

.close-btn:hover {
  background: #f1f3f4;
}

.dialog-form {
  padding: 24px;
}

.form-item {
  margin-bottom: 20px;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-size: 14px;
  font-weight: 500;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
  color: #9ca3af;
}

.input-group input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
  background: #fff;
}

.input-group input:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.input-group input:disabled {
  background: #f5f5f5;
  cursor: not-allowed;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #eee;
}

.dialog-footer button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.cancel-btn {
  border: 1px solid #d1d5db;
  background: #fff;
  color: #4b5563;
}

.cancel-btn:hover {
  background: #f9fafb;
  border-color: #9ca3af;
}

.submit-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  box-shadow: 0 2px 6px rgba(79, 172, 254, 0.3);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.4);
}

@media (max-width: 768px) {
  .members-list {
    padding: 16px;
  }
  
  .filter-section {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .members-table th,
  .members-table td {
    padding: 12px 8px;
    font-size: 13px;
  }
  
  .dialog {
    width: 95vw;
  }
}
</style>